﻿/*<summary>
Created By : Swati
Created On : 10/02/2008
Class Name : PatientRegistration_Mapper
Used In    : 
------------------------------------------------------------------------
Modification Log  
------------------------------------------------------------------------
Created By        Modified By         Remarks                             
-----------       ------------        ----------------------------------
                                                                          
------------------------------------------------------------------------
</summary>*/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Utils;

namespace RegistrationController
{
    public class PatientRegistration_Mapper
    {
        public bool Load_PATIENTREGISTRATION_DATA(PatientRegistration objRegister)
        {
            if (objRegister != null)
            {
                //ezUtilsAdapter DBL = new ezUtilsAdapter();
                bool bresult;
               
                try
                {
                    SqlParameter outPID = new SqlParameter("@vchOutPID", SqlDbType.BigInt);
                    outPID.Direction = ParameterDirection.Output;
                    SqlParameter outCRN = new SqlParameter("@vchOutCRN", SqlDbType.BigInt);
                    outCRN.Direction = ParameterDirection.Output;
                    SqlParameter retval = new SqlParameter("@retVal", SqlDbType.Int);
                    retval.Direction = ParameterDirection.ReturnValue;
                    hpm_ezUtilsAdapter.executeNonQuery("SP_SetPatient",
                    retval,
                    new SqlParameter("@vchPID", objRegister.PatientInfo.PatientID),
                    new SqlParameter("@vchFName", objRegister.PatientInfo.FName),
                    new SqlParameter("@vchMName", objRegister.PatientInfo.MName),
                    new SqlParameter("@vchLName", objRegister.PatientInfo.LName),
                    new SqlParameter("@vchFatherFName", objRegister.PatientInfo.FatherFName),
                    new SqlParameter("@vchFatherMName", objRegister.PatientInfo.FatherMName),
                    new SqlParameter("@vchFatherLName", objRegister.PatientInfo.FatherLName),
                    new SqlParameter("@vchMobilePhone", objRegister.PatientInfo.MobilePhone),
                    new SqlParameter("@vchHomePhone", objRegister.PatientInfo.HomePhone),
                    new SqlParameter("@vchOfficePhone", objRegister.PatientInfo.OfficePhone),
                    new SqlParameter("@vchAddress1", objRegister.PatientInfo.Address1),
                    new SqlParameter("@vchAddress2", objRegister.PatientInfo.Address2),
                    new SqlParameter("@vchCity", objRegister.PatientInfo.City),
                    new SqlParameter("@vchState", objRegister.PatientInfo.State),
                    new SqlParameter("@vchZipCode", objRegister.PatientInfo.ZipCode),
                    new SqlParameter("@vchDOB", new DateTime(DateTime.Now.Year - Convert.ToInt32(objRegister.PatientInfo.Age), DateTime.Now.Month,DateTime.Now.Day)),
                    new SqlParameter("@vchSex", objRegister.PatientInfo.Sex),
                    new SqlParameter("@vchConsultant", objRegister.Consultant),
                    new SqlParameter("@vchPatientType", objRegister.PatientType),
                    new SqlParameter("@vchRegistrationFees", objRegister.RegistrationFees),
                    new SqlParameter("@vchConsultationFees", objRegister.ConsultantFees),
                    new SqlParameter("@vchInitialDeposit", objRegister.InitialDeposit),
                    new SqlParameter("@vchPackage", objRegister.PackageName),
                    outPID,
                    outCRN);
                    if (Convert.ToInt16(retval.Value) == 0) 
                    { 
                        objRegister.PatientInfo.PatientID = Convert.ToString(outPID.Value);
                        objRegister.CRNumber = Convert.ToString(outCRN.Value);
                        bresult = true; 
                    } else 
                    {
                        bresult = false; 
                    } 
                    
                    return bresult;
                }
                catch (SqlException ex)
                {
                    string errorMessage = string.Format(
                        "An Error Occurred While Loading the Patient Information to the DB. Message: {0}", ex.Message);
                    //throw new MappingException(errorMessage, ex);}
                    return false;
                }
                finally
                {
                    //DBL.Dispose();
                }
            }

            else { return false; }
        }

        public bool Shift_OutPatient_to_InPatient(PatientRegistration objRegister)
        {
            if (objRegister != null)
            {
                //ezUtilsAdapter DBL = new ezUtilsAdapter();
                bool bresult;
                int ResultVal;
                ezUtilsAdapter DBL = new ezUtilsAdapter();
                
                try
                {
                    ResultVal = DBL.RunSPReturnInteger("SP_UpdateOpenCase",
                                    new SqlParameter("@biCRNumber", objRegister.CRNumber),
                                    new SqlParameter("@vchConsultant", objRegister.Consultant),
                                    new SqlParameter("@smRegFees", objRegister.RegistrationFees),
                                    new SqlParameter("@mDeposit", objRegister.InitialDeposit),
                                    new SqlParameter("@vchPackage", objRegister.PackageName),
                                    new SqlParameter("@vchPatientType", objRegister.PatientType));
                    
                    if (ResultVal == 0) { bresult = true; } else { bresult = false; } 
                    
                    return bresult;
                }
                catch (SqlException ex)
                {
                    string errorMessage = string.Format(
                        "An Error Occurred While Loading the Patient Information to the DB. Message: {0}", ex.Message);
                    //throw new MappingException(errorMessage, ex);}
                    return false;
                }
                finally
                {
                    DBL.Dispose();
                }
            }

            else { return false; }
        }

        public void Dispose()
        {
            this.Dispose(true);
            System.GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
        }
    }
}